import numpy as np

class FuzzyComprehensiveEvaluation:
    def __init__(self, weights, membership_matrix, grades=None):
        '''
        weights: 权重向量，shape=(n,)
        membership_matrix: 隶属度矩阵，shape=(n, m)
        grades: 评价等级列表，可选
        '''
        self.weights = np.array(weights)
        self.R = np.array(membership_matrix)
        self.grades = grades if grades is not None else [f'V{i+1}' for i in range(self.R.shape[1])]
        self.result = None
        assert self.R.shape[0] == self.weights.shape[0], "权重向量长度必须与隶属度矩阵行数相同"
        assert self.R.shape[1] == len(self.grades), "隶属度矩阵列数必须与评价等级数相同"

    def weighted_average(self):
        '''加权平均算子综合评价'''
        B = np.dot(self.weights, self.R)
        self.result = B
        return B

    def judge(self):
        '''最大隶属度原则，返回最终等级'''
        if self.result is None:
            self.weighted_average()
        idx = np.argmax(self.result)
        return self.grades[idx]

    def print_result(self):
        print("综合评价向量:", self.result)
        print("最终判定等级:", self.judge())

# 用例
if __name__ == "__main__":
	# 示例：3个指标，5个等级
	weights = [0.3, 0.5, 0.2]
	R = [
		[0.1, 0.2, 0.5, 0.1, 0.1],
		[0.2, 0.3, 0.3, 0.1, 0.1],
		[0.3, 0.4, 0.2, 0.05, 0.05]
	]
	grades = ['优', '良', '中', '及格', '差']
	fce = FuzzyComprehensiveEvaluation(weights, R, grades)
	fce.weighted_average()
	fce.print_result()
